package re.kitri.common.member;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import re.kitri.common.board.BoardDAO;
import re.kitri.common.board.BoardDTO;
import re.kitri.common.game.GameDTO;
import re.kitri.common.game.GameImple;

@WebServlet("/memberservlet")
public class memberservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		myFunction(request, response);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("euc-kr");
		myFunction(request, response);
	}
	
	protected void myFunction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		MemberDAO dao = new MemberDAO();
		MemberDTO dto = new MemberDTO();
		
		if(action.equals("JOIN")){
			String name = request.getParameter("name");
			String id = request.getParameter("id");
			String pw = request.getParameter("pw");
			String pwcheck = request.getParameter("pwcheck");
			String code1 = request.getParameter("code1");
			String code2 = request.getParameter("code2");
			String address = request.getParameter("addr");
			String addrplus = request.getParameter("addrplus");
			String telv = request.getParameter("telvalue");
			String tel1 = request.getParameter("tel1");
			String tel2 = request.getParameter("tel2");
			String tel = telv+tel1+tel2;
			String addr = code1+"-"+code2+" "+address + " " + addrplus;
			
			String emailn = request.getParameter("email");
			String email = emailn+request.getParameter("emailvalue");
			
			String bank = request.getParameter("bank");
			String bankN = request.getParameter("account");
			long account = Long.parseLong(bankN);
			
			
			
			dto.setName(name);
			dto.setId(id);
			dto.setPw(pw);
			dto.setAddress(addr);
			dto.setTel(tel);
			dto.setEmail(email);
			dto.setBank(bank);
			dto.setAccount(account);
			
			dao.Join(dto);
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_joinOk.jsp");
			rd.forward(request, response);

		}else if(action.equals("JUNGBOK")){
			String id = request.getParameter("id");
			dto.setId(id);
			String idcheck = dto.getId();
			
			System.out.println("JUNGBOK : " +id);
			
			String dMsg = "";
			dMsg = dao.jungbok(dto);
			
			request.setAttribute("MSG", dMsg);
			request.setAttribute("id", idcheck);
			
			HttpSession session = request.getSession();
			session.setAttribute("CHECK_USERID", id);
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_idcheckOk.jsp");
			rd.forward(request, response);
		
		}else if(action.equals("LIST")){
			ArrayList cList = (ArrayList)dao.customList();
			
			request.setAttribute("cList", cList);
			
			RequestDispatcher rd= request.getRequestDispatcher("/member/member_cList.tiles");
			rd.forward(request, response);
		
		}else if(action.equals("UPDATE")){
			int account = 0;
			String pw = request.getParameter("pw");
			String address = request.getParameter("address");
			String tel = request.getParameter("tel");
			String email = request.getParameter("email");
			String banknum = request.getParameter("account");
			account = Integer.parseInt(banknum);
			String bank = request.getParameter("bank");
			String id = request.getParameter("id");
			
			dto.setPw(pw);
			dto.setAddress(address);
			dto.setTel(tel);
			dto.setEmail(email);
			dto.setAccount(account);
			dto.setBank(bank);
			dto.setId(id);
			
			dao.customUpdate(dto);
			
			response.setCharacterEncoding("euc-kr");
			response.sendRedirect("/member/member_uresult.tiles");
			
	
		}else if(action.equals("LOGIN")){
			String id = request.getParameter("id");
			String pw = request.getParameter("pw");
			dto.setId(id);
			dto.setPw(pw);

			
			MemberDTO dtoL = dao.login(dto);
			
			String returnName = dtoL.getName();
			String returnId = dtoL.getId();
			int seq = dtoL.getSeq();
			
			
			System.out.println("LOGIN returnName : " + returnName);
			System.out.println("LOGIN returnSeq : " + seq);
			String url = "";

			if(returnName == null||returnName.equals("")){
				url="/top/member_login_error.tiles";
			
			}else{
					url="/top/member_normal.tiles";
					HttpSession session = request.getSession();
					session.setAttribute("SESS_USERSEQ", seq);
					session.setAttribute("SESS_USERID", id);
					session.setAttribute("SESS_USERNAME", returnName);
				}
				
			GameDTO gameDTO = new GameDTO();
			GameImple gameImple = new GameImple();
			
			BoardDAO boardDao = new BoardDAO();
			BoardDTO boardDto = new BoardDTO(); 
			ArrayList list = boardDao.MainList(boardDto);
			
			request.setAttribute("gameList", gameImple.listSummary());
			request.setAttribute("Main_BOARDLIST", list);
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
			
		}else if(action.equals("DVEIW")){

			HttpSession session = request.getSession();
			String id = session.getAttribute("SESS_USERID").toString();
			dto.setId(id);
			
			ArrayList list = (ArrayList)dao.custom(dto);
			request.setAttribute("uLIST", list);
			
			RequestDispatcher rd= request.getRequestDispatcher("/member/member_update.tiles");
			rd.forward(request, response);
		
		}else if(action.equals("LOGOUT")){
			
			HttpSession session = request.getSession();
			session.invalidate();
			 
			session.setMaxInactiveInterval(0);
			
			Cookie cookie = new Cookie("name","");
			cookie.setMaxAge(0);
			response.addCookie(cookie);			
			
			response.sendRedirect("index.jsp");
		
		}else if(action.equals("ZIPCHECK")){
			String zip = request.getParameter("addrzip");
			dto.setDong(zip);
			
			System.out.println("ZIPCHECK : " +zip);
	
			ArrayList sList = dao.searchCode(dto);
			request.setAttribute("sList", sList);
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_join_zipOK.jsp");
			rd.forward(request, response);
			

		}else if(action.equals("DELETE")){
			String id = request.getParameter("id");
			dto.setId(id);
			
			dao.memDelete(dto);
			
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_delete.tiles");
			rd.forward(request, response);
		
		}else if(action.equals("FINDID")){
			String name = request.getParameter("name");
			String email = request.getParameter("email");
			
			System.out.println("FINDID name : " +name);
			System.out.println("FINDID email : " +email);
			dto.setName(name);
			dto.setEmail(email);
			dto = (MemberDTO)dao.findId(dto);
			
			request.setAttribute("FIDDTO", dto);
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_findidok.jsp");
			rd.forward(request, response);
		
		}else if(action.equals("FINDPW")){
			String id = request.getParameter("id");
			String banknum = request.getParameter("account");
			long account = Long.parseLong(banknum);
			
			dto.setId(id);
			dto.setAccount(account);
			
			dto = (MemberDTO)dao.findPw(dto);
			
			request.setAttribute("FPWDTO", dto);
			
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_findpwok.jsp");
			rd.forward(request, response);
			
		}else if(action.equals("HONOR")){
			RequestDispatcher rd = request.getRequestDispatcher("/member/member_honor.tiles");
			rd.forward(request, response);
		}
	}

}

